package ru.eas.packer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author aesipov
 */
public class Solution {

    private PositionedBox container;
    private List<PositionedBox> boxes;
    private List<Point> points;
    private int quality;

    public Solution(PositionedBox container, List<PositionedBox> boxes, List<Point> points, int quality) {
        this.quality = quality;
        this.container = container;
        this.boxes = boxes;
        this.points = points;
    }

    public boolean validateNewBox(PositionedBox box) {
        if (container.isIntersect(box)) {
            return false;
        }
        for (PositionedBox positionedBox : boxes) {
            if (positionedBox.isIntersect(box)) {
                return false;
            }
        }
        //check that box doen't lay on top only
        return true;
    }

    public Solution newSolution(PositionedBox box) {
        Solution result = new Solution(container, new ArrayList<PositionedBox>(boxes), new ArrayList<Point>(points), quality);
        result.boxes.add(box);
        result.points.add(box.getTopPoint());
        result.points.add(box.getLeftPoint());
        result.points.add(box.getRightPoint());
        result.quality += box.getBox().getVolume();
        return result;
    }

    public List<Point> getPoints() {
        return points;
    }

    public int getQuality() {
        return quality;
    }
}
